package com.adgox.tiantianbiting.mine.message;

import android.widget.Toast;

import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.MessageBean;
import com.adgox.tiantianbiting.bean.MessageCountBean;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.adgox.tiantianbiting.utils.SpUtils;
import com.adgox.tiantianbiting.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.Date;

public class MessagePresenterImpl implements MessageContract.Presenter {

    private MessageContract.View mView;
    private int userId = SessionManager.getDefault().getUser() == null?0:((UserInfo)SessionManager.getDefault().getUser()).getId();
//    private int userId = 1;



    public MessagePresenterImpl(MessageContract.View view) {
        this.mView=view;
        mView.setPresenter(this);
    }

    @Override
    public void getSystem(int startPage,int maxPage,int type) {
//        0.系统消息 1.活动消息 2.评论消息
        OkGo.<BaseResponseBean<MessageBean.DataBeanX>>get(ApiConstant.BASE_URL+ApiConstant.MY_MESSAGE_LIST)
                .params("userId", userId)
                .params("startPoint",startPage)
                .params("maxPage",maxPage)
                .params("type",type)
                .execute(new JsonCallback<BaseResponseBean<MessageBean.DataBeanX>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<MessageBean.DataBeanX>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())){
                            mView.onGetDataFailed(response.body().getMessage());
                            return;
                        }
                        mView.setSystem(response.body().getData());
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<MessageBean.DataBeanX>> response) {
                        super.onError(response);
                        DialogUtils.dismiss();
                    }
                });
    }

    @Override
    public void getSystemCount(long startTime, final int type) {
        if (startTime==0){
            OkGo.<BaseResponseBean<MessageCountBean.DataBean>>get(ApiConstant.BASE_URL+ApiConstant.MY_MESSAGE_UNREAD_LIST)
                    .params("userId", userId)
                    .params("type",type)
                    .execute(new JsonCallback<BaseResponseBean<MessageCountBean.DataBean>>() {
                        @Override
                        public void onSuccess(Response<BaseResponseBean<MessageCountBean.DataBean>> response) {
                            DialogUtils.dismiss();
                            if (!"0".equals(response.body().getCode())){
                                mView.onGetDataFailed(response.body().getMessage());
                                return;
                            }
                            if (type==0){
                                mView.setSystemCount(response.body().getData());
                            }else{
                                mView.setActivityCount(response.body().getData());

                            }
                        }

                        @Override
                        public void onError(Response<BaseResponseBean<MessageCountBean.DataBean>> response) {
                            super.onError(response);
                            DialogUtils.dismiss();
                        }
                    });
        }else{
            OkGo.<BaseResponseBean<MessageCountBean.DataBean>>get(ApiConstant.BASE_URL+ApiConstant.MY_MESSAGE_UNREAD_LIST)
                    .params("userId", userId)
                    .params("type",type)
                    .params("startTime",startTime)
                    .execute(new JsonCallback<BaseResponseBean<MessageCountBean.DataBean>>() {
                        @Override
                        public void onSuccess(Response<BaseResponseBean<MessageCountBean.DataBean>> response) {
                            DialogUtils.dismiss();
                            if (!"0".equals(response.body().getCode())){
                                mView.onGetDataFailed(response.body().getMessage());
                                return;
                            }
                            if (type==0){
                                mView.setSystemCount(response.body().getData());
                            }else{
                                mView.setActivityCount(response.body().getData());

                            }
                        }

                        @Override
                        public void onError(Response<BaseResponseBean<MessageCountBean.DataBean>> response) {
                            super.onError(response);
                            DialogUtils.dismiss();
                        }
                    });
        }


    }

    @Override
    public void getActivity(int startPage,int maxPage,int type) {
        OkGo.<BaseResponseBean<MessageBean.DataBeanX>>get(ApiConstant.BASE_URL+ApiConstant.MY_MESSAGE_LIST)
                .params("userId", userId)
                .params("startPoint",startPage)
                .params("maxPage",maxPage)
                .params("type",type)
                .execute(new JsonCallback<BaseResponseBean<MessageBean.DataBeanX>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<MessageBean.DataBeanX>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())){
                            mView.onGetDataFailed2(response.body().getMessage());
                            return;
                        }
                        mView.setActivity(response.body().getData());
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<MessageBean.DataBeanX>> response) {
                        super.onError(response);
                        DialogUtils.dismiss();
                    }
                });
    }

    @Override
    public void getComment(int startPage,int maxPage,int type) {
        OkGo.<BaseResponseBean<MessageBean.DataBeanX>>get(ApiConstant.BASE_URL+ApiConstant.MY_MESSAGE_LIST)
                .params("userId", userId)
                .params("startPoint",startPage)
                .params("maxPage",maxPage)
                .params("type",type)
                .execute(new JsonCallback<BaseResponseBean<MessageBean.DataBeanX>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<MessageBean.DataBeanX>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())){
                            mView.onGetDataFailed(response.body().getMessage());
                            return;
                        }
                        mView.setComment(response.body().getData());
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<MessageBean.DataBeanX>> response) {
                        super.onError(response);
                        DialogUtils.dismiss();
                    }
                });
    }

    @Override
    public void start() {

    }
}
